package asteroids.statements;


import java.util.*;

import be.kuleuven.cs.som.annotate.Basic;

import asteroids.Ship;
import asteroids.expressions.Expression;
import asteroids.types.Type;

public class AssignmentStatement extends BasicStatement {
	
	private String variable;
	private Expression rightHandSide;

	public AssignmentStatement(int line, int column, String variable, Expression rhs) {
		super(line, column);
		this.variable = variable;
		this.rightHandSide = rhs;
	}
	
	@Basic
	public String getVariable() {
		return variable;
	}
	
	@Basic
	public Expression getExpression() {
		return rightHandSide;
	}

	@Override
	public void execute(Ship ship) {
		if(ship.getProgram().getGlobals().containsKey(getVariable())){
			ship.getProgram().getGlobals().put(getVariable(), getExpression().evaluate(ship, ship.getProgram().getGlobals()));
		}
	}
	
	@Override
	public List<String> getTypeCheckErrors(List<String> typeErrors, Map<String, Type> globalVariables) {
		if(!globalVariables.containsKey(getVariable())){
			typeErrors.add("Line: " + getLine() + " Column: " + getColumn() + " - There is no global variable with variable name '"+ getVariable() +"'.");
		}
		if(globalVariables.get(getVariable()).getClass() != getExpression().evaluate(null, globalVariables).getClass())
			typeErrors.add("Line: " + getLine() + " Column: " + getColumn() + " - The type of the expression '"+ getExpression().toString() +"' is not the same as the type of the global variable '"+ getVariable() +"'.");
		typeErrors = getExpression().getTypeCheckErrorsExpressions(typeErrors, globalVariables);
		return typeErrors;
	}

	@Override
	public boolean hasActionStatement() {
		return false;
	}

}
